package com.elitel.frame.base.service.impl;

import com.elitel.frame.base.service.AcStationInfoService;
import com.elitel.frame.main.dao.AcAppCatalogMapper;
import com.elitel.frame.main.dao.AcAppMapper;
import com.elitel.frame.main.dao.AcAppStationMapper;
import com.elitel.frame.main.dao.AcStationInfoMapper;
import com.elitel.frame.main.dao.ext.ExtAcStationInfoMapper;
import com.elitel.frame.main.entity.AcApp;
import com.elitel.frame.main.entity.acappcatalog.AcAppCatalog;
import com.elitel.frame.main.entity.acappcatalog.AcAppCatalogExample;
import com.elitel.frame.main.entity.acappstation.AcAppStation;
import com.elitel.frame.main.entity.acappstation.AcAppStationExample;
import com.elitel.frame.main.entity.acstationinfo.AcStationInfo;
import com.elitel.frame.main.entity.acstationinfo.AcStationInfoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 访问控制-测站信息表(AcStationInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-11-09 15:02:43
 */
@Service("acStationInfoService")
public class AcStationInfoServiceImpl implements AcStationInfoService {
    @Resource
    private AcStationInfoMapper acStationInfoMapper;
    @Resource
    private AcAppStationMapper acAppStationMapper;
    @Resource
    private AcAppMapper acAppMapper;
    @Resource
    private AcAppCatalogMapper acAppCatalogMapper;

    @Resource
    private ExtAcStationInfoMapper extAcStationInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param stcd 主键
     * @return 实例对象
     */
    @Override
    public AcStationInfo queryById(String stcd) {
        return this.acStationInfoMapper.selectByPrimaryKey(stcd);
    }


    /**
     * 新增数据
     *
     * @param acStationInfo 实例对象
     * @return 实例对象
     */
    @Override
    public AcStationInfo insert(AcStationInfo acStationInfo) {
        this.acStationInfoMapper.insert(acStationInfo);
        return acStationInfo;
    }

    /**
     * 修改数据
     *
     * @param acStationInfo 实例对象
     * @return 实例对象
     */
    @Override
    public AcStationInfo update(AcStationInfo acStationInfo) {
        this.acStationInfoMapper.updateByPrimaryKeySelective(acStationInfo);
        return this.queryById(acStationInfo.getStcd());
    }

    /**
     * 通过主键删除数据
     *
     * @param stcd 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String stcd) {
        return this.acStationInfoMapper.deleteByPrimaryKey(stcd) > 0;
    }

    @Override
    public PageInfo getDataPage(PageInfo pageInfo, AcStationInfo stationInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<AcStationInfo> resultList = extAcStationInfoMapper.selectList(stationInfo);
        return new PageInfo<>(resultList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(AcStationInfo stationInfo) {
        checkParams(stationInfo);
        stationInfo.setModitime(new Date());
        String stcd = stationInfo.getStcd();
        AcStationInfo acStationInfo = this.acStationInfoMapper.selectByPrimaryKey(stcd);
        if (acStationInfo == null) {
            this.acStationInfoMapper.insertSelective(stationInfo);
        } else {
            this.acStationInfoMapper.updateByPrimaryKeySelective(stationInfo);
        }
    }

    private void checkParams(AcStationInfo stationInfo) {
        String optType = stationInfo.getOptType();
        // 如果不是编辑 就是新增数据 此时需要判断新增的编码是否重复
        if (optType.equals(AcStationInfo.OptType.EDIT.toString())) {
            return;
        }
        String stcd = stationInfo.getStcd();
//        BigDecimal lgtd = stationInfo.getLgtd();
//        BigDecimal lttd = stationInfo.getLttd();
//        int scale = lgtd.scale();



        
//        if (){
//
//        }

        AcStationInfo acStationInfo = this.acStationInfoMapper.selectByPrimaryKey(stcd);
        if (acStationInfo != null) {
            throw new RuntimeException("新增时输入的测站编码已存在!");
        }
    }

    @Override
    public AcStationInfo selectById(AcStationInfo stationInfo) {
        AcStationInfo acStationInfo = this.acStationInfoMapper.selectByPrimaryKey(stationInfo.getStcd());
        return acStationInfo;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(String ids) {
        // 在删除之前要看有没有分组绑定当前测站
        this.checkData(ids);
        AcStationInfoExample example = new AcStationInfoExample();
        List<String> strings = Arrays.asList(ids.split(","));
        example.createCriteria().andStcdIn(strings);
        this.acStationInfoMapper.deleteByExample(example);
    }

    private void checkData(String ids) {
        if (StringUtils.isEmpty(ids)) {
            throw new RuntimeException("删除的站点不能为空");
        }
        AcAppStationExample example = new AcAppStationExample();
        example.createCriteria().andStcdIn(Arrays.asList(ids.split(",")));
        List<AcAppStation> acAppStations = acAppStationMapper.selectByExample(example);
        // 如果没有查询到数据 正常删除
        if (CollectionUtils.isEmpty(acAppStations)) {
            return;
        }

        List<String> caIds = acAppStations.stream().map(AcAppStation::getCatalogId).filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
        AcAppCatalogExample caexa = new AcAppCatalogExample();
        caexa.createCriteria().andCatalogIdIn(caIds);
        List<AcAppCatalog> acAppCatalogs = acAppCatalogMapper.selectByExample(caexa);


        List<String> appIds = acAppCatalogs.stream().map(AcAppCatalog::getAppId).collect(Collectors.toList());
        List<AcApp> acApps = acAppMapper.selectByPrimaryKeys(appIds);
        Map<String, String> appIdNameMap = acApps.stream().collect(Collectors.toMap(AcApp::getAppId, AcApp::getAppName, (r1, r2) -> r1));

        String collect = acAppCatalogs.stream().map(i -> appIdNameMap.get(i.getAppId()) + "的" + i.getCatalogName()).distinct().collect(Collectors.joining(","));
        // 如果查询到了 就需要提示
        throw new RuntimeException("当前测站已经被【" + collect + "】绑定,请从分组中移除后再进行删除");
    }

    @Override
    public PageInfo getDataPageByCatalogId(PageInfo pageInfo, AcStationInfo stationInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<AcStationInfo> resultList = extAcStationInfoMapper.selectByCatalogId(stationInfo);
        return new PageInfo<>(resultList);
    }

    
}
